class Node {
    constructor(element) {
        this.e = element
        this.next = null
    }
}

class LinkListQueue {
    constructor() {
        this.head = null
        this.tail = null
        this.size = 0
    }

    isEmpty() {
        return this.size === 0
    }

    getSize() {
        return this.size
    }

    enqueue(element) {
        if (this.isEmpty()) {
            this.head = new Node(element)
            this.tail = this.head
            
        }else {
            this.tail.next = new Node(element)
            this.tail = this.tail.next
            
        }
        this.size += 1
    }

    dequeue(element) {
        if (this.isEmpty()) {
            throw Error("队列为空")
        }else if (this.getSize() === 1) {
            const returnNode = this.head
            this.head = null
            this.tail = null
            this.size -= 1
            return returnNode.e
        }else {
            const returnNode = this.head
            this.head = returnNode.next
            returnNode.next = null
            this.size -= 1
            return returnNode.e
        }
    }

    peek() {
        if (this.isEmpty()) {
            throw Error("队列为空")
        }else {
            return this.head.e
        }
    }
}

export {LinkListQueue}